import { sign, verify } from 'jsonwebtoken'

export interface ITokenUserInfo {
    id: number
    userId: string
    name: string
    role: 'admin' | 'teacher' | 'student' | 'zhujiao'
}

const secret = 'tcds-token-AdfhDv'

export const generateToken = (user: ITokenUserInfo) => {
    const token = sign(
        user,
        secret,
        {
            expiresIn: '1d',
        }
    )
    return token
}

export const verifyToken = (token: string) => {
    try {
        const user = verify(token, secret) as ITokenUserInfo
        return user
    } catch (error) {
        return null
    }
}